package main

import (
	"fmt"
	"net"
	"time"
)

// 模拟客户端

func main() {
	fmt.Println("Zinx Client V0.1 Start...")

	time.Sleep(1 * time.Second)
	// 1 直接连接远程服务器 得到conn
	conn, err := net.Dial("tcp", "127.0.0.1:8999")
	if err != nil {
		fmt.Printf("client start err: %s\n", err)
		return
	}
	fmt.Println("client start success, ", conn)
	// 2 向服务器写数据
	var s string
	for {
		fmt.Scan(&s)
		cnt, err := conn.Write([]byte(s))
		if err != nil {
			fmt.Printf("write data error: %s\n", err)
			return
		}
		buf := make([]byte, 512)
		cnt, err = conn.Read(buf)
		if err != nil {
			fmt.Printf("read data error: %s\n", err)
			return
		}
		fmt.Printf("read data: %s, cnt = %d\n", buf[:cnt], cnt)
	}
	fmt.Println("Client exit.")
}
